package com.listeningframework.boot.codegen.context;

import com.listeningframework.boot.codegen.utils.CodeGenUtils;
import org.apache.commons.lang3.StringUtils;

import java.util.Date;

/**
 * @author lixining
 * @version $Id: MetaData.java, v 0.1 2016年12月2日 下午5:06:20 lixining Exp $
 */
public class TableMetaData {
    /**
     * 属性名称
     */
    private String javaName;
    /**
     * 属性类型
     */
    private Class<?> javaTypeClass;
    /**
     * 数据库字段名称
     */
    private String columnName;
    /**
     * 数据库字段类型名称
     */
    private String columnType;
    /**
     * 属性描述
     */
    private String comment;

    /**
     * Getter method for property <tt>javaName</tt>.
     *
     * @return property value of javaName
     */
    public String getJavaName() {
        return javaName;
    }

    /**
     * Setter method for property <tt>javaName</tt>.
     *
     * @param javaName value to be assigned to property javaName
     */
    public void setJavaName(String javaName) {
        this.javaName = javaName;
    }

    /**
     * Getter method for property <tt>javaTypeClass</tt>.
     *
     * @return property value of javaTypeClass
     */
    public Class<?> getJavaTypeClass() {
        return javaTypeClass;
    }

    /**
     * Setter method for property <tt>javaTypeClass</tt>.
     *
     * @param javaTypeClass value to be assigned to property javaTypeClass
     */
    public void setJavaTypeClass(Class<?> javaTypeClass) {
        this.javaTypeClass = javaTypeClass;
    }

    /**
     * Getter method for property <tt>columnName</tt>.
     *
     * @return property value of columnName
     */
    public String getColumnName() {
        return columnName;
    }

    /**
     * Setter method for property <tt>columnName</tt>.
     *
     * @param columnName value to be assigned to property columnName
     */
    public void setColumnName(String columnName) {
        this.columnName = columnName;
    }

    /**
     * Getter method for property <tt>columnType</tt>.
     *
     * @return property value of columnType
     */
    public String getColumnType() {
        return columnType;
    }

    /**
     * Setter method for property <tt>columnType</tt>.
     *
     * @param columnType value to be assigned to property columnType
     */
    public void setColumnType(String columnType) {
        this.columnType = columnType;
    }

    /**
     * Getter method for property <tt>comment</tt>.
     *
     * @return property value of comment
     */
    public String getComment() {
        return comment;
    }

    /**
     * Setter method for property <tt>comment</tt>.
     *
     * @param comment value to be assigned to property comment
     */
    public void setComment(String comment) {
        this.comment = comment;
    }

    /**
     * 返回类名
     */
    public String getJavaTypeClassSimpleName() {
        return javaTypeClass.getSimpleName();
    }

    /**
     * 当前属性是否为日期数据类型
     *
     * @return boolean
     */
    public boolean isDateType() {
        return CodeGenUtils.isType(javaTypeClass, Date.class);
    }

    /**
     * 是否为String
     *
     * @return boolean
     */
    public boolean isStringType() {
        return CodeGenUtils.isType(javaTypeClass, String.class);
    }

    /**
     * 生成Model对象的Getter方法名称
     *
     * @return String
     */
    public String getGetterMethodName() {
        return "get" + StringUtils.capitalize(getModelFieldName());
    }

    /**
     * 生成Model对象的Setter方法名称
     *
     * @return String
     */
    public String getSetterMethodName() {
        return "set" + StringUtils.capitalize(getModelFieldName());
    }

    /**
     * 生成Model对象的属性名称
     *
     * @return String
     */
    public String getModelFieldName() {
        return isDateType() ? (javaName + "Str") : javaName;
    }

    /**
     * 生成Model对象的属性类型
     *
     * @return String
     */
    public String getModelFieldType() {
        return isDateType() ? "String" : javaTypeClass.getSimpleName();
    }

    /**
     * Mapper Test Condition
     *
     * @return String
     */
    public String getMapperCondition() {
        StringBuilder builder = new StringBuilder();
        builder.append(javaName).append(" != null");
        if (isStringType()) {
            builder.append(" and ").append(javaName).append(" != ''");
        }
        return builder.toString();
    }

    /**
     * 是否ID
     */
    public boolean isKey() {
        return StringUtils.equalsIgnoreCase(javaName, "id");
    }
}
